package com.example.weatherforecast.whtrcdn.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.weatherforecast.R
import com.example.weatherforecast.whtrcdn.WhtrcdnViewModel
import com.example.weatherforecast.whtrcdn.adapter.WeatherCityManagerListAdapter
import kotlinx.android.synthetic.main.activity_weather_city_manager.*

class WeatherCityManagerActivity : AppCompatActivity() {

    private var viewModel: WhtrcdnViewModel ?= null
    private var cityRecyclerView: RecyclerView ?= null
    private var cityManagerListAdapter: WeatherCityManagerListAdapter ?= null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_weather_city_manager)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        viewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory(application)).get(WhtrcdnViewModel::class.java)
        cityRecyclerView = findViewById(R.id.rv_weather_city)

        cityRecyclerView?.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        cityManagerListAdapter = viewModel?.getCurrentWeatherCityInfoList()?.let {
            WeatherCityManagerListAdapter(this, it, viewModel!!)
        }
        cityRecyclerView?.adapter = cityManagerListAdapter
        fab_weather_city_add.setOnClickListener {
            startActivity(Intent(this, WeatherCityAddActivity::class.java))
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel?.getCurrentWeatherCityInfoList()?.let {
            cityManagerListAdapter?.updateData(it)
            cityManagerListAdapter?.notifyDataSetChanged()
        }
    }

}